import { slice, deleteExtraProps, isType, isArray } from '../utils'

function filterSpaceChildren(children) {
    if (isArray(children)) {
        return children.filter(item => {
            if (isType(item, 'string')) {
                return item.replace(/\s*/, '');
            }
            return true;
        })
    }
    return [];
}

/**
 * 创建元素
 * @param {*} type 
 * @param {*} props 
 * @param  {...any} children 
 */
export function createElement(type, props, ...children) {
    // 删除不要的属性
    deleteExtraProps(props, '__self');
    deleteExtraProps(props, '__source');
    // 多个孩子，变成数组
    if (children.length > 1) {
        children = slice(children);
        children = filterSpaceChildren(children);
    }
    if (children.length === 1) {
        children = children[0]
    }
    props.children = children;
    return {
        type,
        props
    }
}

